<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;
    const POSTONLY = [
        'name','avatar','password','mobile','role_id','company_id','sex','openid','idcard','ip','status','basic_salary','posts','formal',
        'birth_date','id_type','ethnicity','political_status','education','household_register','residential_address','contract_type','contract_start_date',
        'contract_end_date','trial_start_period','trial_end_period','employees','bank','bank_number','bank_account'
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name','avatar','password','mobile','role_id','company_id','sex','openid','idcard','ip','status','basic_salary','posts','formal',
        'birth_date','id_type','ethnicity','political_status','education','household_register','residential_address','contract_type','contract_start_date',
        'contract_end_date','trial_start_period','trial_end_period','employees','bank','bank_number','bank_account'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'status' => 'boolean',
    ];

    protected $appends = [
        'status_text','formal_text','employees_text'
    ];

    public function getStatusTextAttribute()
    {
        return $this->status ? '启用' : '禁用';
    }

    public function getFormalTextAttribute()
    {
        return $this->formal ? '编内' : '编外';
    }

    public function getEmployeesTextAttribute()
    {
        return $this->employees == 1 ? '在职' : '离职';
    }

    /**
     * 验证用户的密码以获得 Passport 密码授权。
     *
     * @param  string  $password
     * @return bool
     */
    public function validateForPassportPasswordGrant($password)
    {
        return Hash::check($password, $this->password);
    }

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    // role
    public function roles()
    {
        // return $this->belongsTo(Role::class)->select('id','name','type');
        // return $this->belongsToMany(Project::class,'project_users')->withPivot('role_id','start_date','end_date');
        // return $this->hasMany(ProjectUser::class,'user_id','id');
        return $this->hasMany(RoleUser::class,'user_id','id');
    }

    // more
    public function auths()
    {
        return $this->hasOne(UserAuth::class);
    }

    // more
    public function more()
    {
        return $this->hasOne(UserMore::class);
    }

    // more
    public function edus()
    {
        return $this->hasMany(UserEdu::class);
    }

    public function projects()
    {
        return $this->belongsToMany(Project::class,'project_users');
    }

    public function certs()
    {
        return $this->hasMany(Cert::class);
    }

    public function imprestPrice()
    {
        return $this->hasMany(Application::class,'user_id','id')->where('model','Imprest')->where('status','approved')->orderBy('id','desc');
    }

    public function company()
    {
        return $this->hasOne(Company::class,'id','company_id')->select('id','title','logo');
    }

}
